package cn.easybuy.dao.impl;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.OrderDAO;
import cn.easybuy.entity.*;
import com.sun.org.apache.bcel.internal.generic.ANEWARRAY;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OrderDAOImpl extends BaseDao implements OrderDAO {
    @Override
    public List<Order> SelOrder(int id) throws SQLException {
        List list = new ArrayList();
        String sql = "";
        Object[] obj ;
        if (id == 0){
            sql = "select * from easybuy_order";
            obj = new Object[0];
        }else{
            sql = "select * from easybuy_order where userId = ?";
            obj = new Object[1];
            obj[0] = id;
        }
        ResultSet rs = executeQuery(sql, obj);
        while (rs.next()){
            int id1 = rs.getInt("id");
            String loginName = rs.getString("loginName");
            String userAddress = rs.getString("userAddress");
            float cost = rs.getFloat("cost");
            String serialNumber = rs.getString("serialNumber");
            Order order = new Order(id1,loginName,userAddress,cost,serialNumber);
            list.add(order);
        }
        return list;
    }

    @Override
    public List<OrderShow> SelOrderPar(String serialNumber) throws SQLException {
        List<OrderShow> list = new ArrayList<>();
        String sql = "select p.name,p.fileName,od.quantity,od.cost,o.`id` " +
                "from easybuy_product p,easybuy_order_detail od,easybuy_order o ,easybuy_user u " +
                "where p.`id`=od.`productId`and od.orderId=o.id and o.`userId`=u.`id` and serialNumber=?";
        Object[] obj = new Object[1];
        obj[0] = serialNumber;
        ResultSet rs = executeQuery(sql, obj);
        while (rs.next()){
            String name = rs.getString("name");
            String fileName = rs.getString("fileName");
            int quantity = rs.getInt("quantity");
            float cost = rs.getFloat("cost");
            int id1 = rs.getInt("id");
            OrderShow orderShow = new OrderShow(id1, name, fileName, quantity, cost);
            list.add(orderShow);
        }
        return list;
    }

    @Override
    public List<UserAddress> SelUserAddress(int id) throws SQLException {
        List<UserAddress> list = new ArrayList<>();
        String sql = "select * from easybuy_user_address where userId = ?";
        Object[] obj = new Object[1];
        obj[0] = id;
        ResultSet rs = executeQuery(sql, obj);
        while (rs.next()){
            int id1 = rs.getInt("id");
            int userId = rs.getInt("userId");
            String address = rs.getString("address");
            String remark = rs.getString("remark");
            UserAddress userAddress = new UserAddress();
            userAddress.setId(id1);
            userAddress.setUserId(userId);
            userAddress.setAddress(address);
            userAddress.setRemark(remark);
            list.add(userAddress);
        }

        return list;
    }

    @Override
    public int AddUserAddress(UserAddress userAddress) throws SQLException {
        String sql = "insert into easybuy_user_address value(null,?,?,null,0,?);";
        Object[] obj = new Object[3];
        obj[0] = userAddress.getUserId();
        obj[1] = userAddress.getAddress();
        obj[2] = userAddress.getRemark();
        return executeUpdate(sql,obj);
    }

    @Override
    public int AddOrder(Order order) throws SQLException {
        String sql = "insert into easybuy_order value (null,?,?,?,now(),?,?)";
        Object[] obj = new Object[5];
        obj[0] = order.getUserId();
        obj[1] = order.getLoginName();
        obj[2] = order.getUserAddress();
        obj[3] = order.getCost();
        obj[4] = order.getSerialNumber();
        return executeUpdate(sql, obj);
    }

    @Override
    public int selOrder(String serialNumber) throws SQLException {
        String sql = "select * from easybuy_order where serialNumber = ?";
        Object[] obj = new Object[1];
        obj[0] = serialNumber;
        ResultSet rs = executeQuery(sql, obj);
        int id = 0;
        if (rs.next()){
            id = rs.getInt("id");
        }
        return id;
    }

    @Override
    public int AddDatail(OrderDetail orderDetail) throws SQLException {
        String sql = "insert into easybuy_order_detail value(null,?,?,?,?)";
        Object[] obj = new Object[4];
        obj[0] = orderDetail.getOrderId();
        obj[1] = orderDetail.getProductId();
        obj[2] = orderDetail.getQuantity();
        obj[3] = orderDetail.getCost();
        return executeUpdate(sql,obj);
    }

/*    @Override
    public PageInfo<Order> listPage(int id,PageInfo<Order> pageInfo) throws SQLException {
        List<Order> list = new ArrayList<>();
        String sql = "";
        Object[] obj ;
        if (id == 0){
            sql = "select * from easybuy_order limit ?,?";
            obj = new Object[2];
        }else{
            sql = "select * from easybuy_order where userId = ? limit ?,?";
            obj = new Object[3];
        }
        ResultSet rs = executeQuery(sql, obj);
        while (rs.next()){
            int id1 = rs.getInt("id");
            String loginName = rs.getString("loginName");
            String userAddress = rs.getString("userAddress");
            float cost = rs.getFloat("cost");
            String serialNumber = rs.getString("serialNumber");
            Order order = new Order(id1,loginName,userAddress,cost,serialNumber);
            list.add(order);
        }
        pageInfo.setPageList(list);
        if (id == 0){
            sql = "select count(*) from easybuy_order";
            obj = new Object[0];
        }else{
            sql = "select count(*) from easybuy_order where userId = ?";
            obj = new Object[1];
        }
        executeQuery(sql,obj)
//        // 总记录条数    总页数
//        String sqlCount="select count(*) total from newsinfo";
//        ResultSet rs2 = exeQuery(sqlCount, null);
//        int count=0;
//        if(rs2.next()){
//            count = rs2.getInt("total");
//        }
//        //计算总页数
//        int size=page.getPageSize();
//        //count 是总记录条数， size 是每页显示的条数  num是总页数
//        int num = count%size==0 ? count/size : count/size+1;
//
//        page.setPageNum(num);
//        page.setPageCount(count);
//
//        return page;

    }*/
}
